package com.baseframe.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.auth.entity.UserBean;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.log.exception.ServiceException;
import com.baseframe.user.entity.BaseUser;
import com.baseframe.user.entity.SnsUserRelation;
import com.baseframe.user.entity.request.SnsUserRelationRequest;
import com.baseframe.user.entity.vo.BaseUserVO;
import com.baseframe.user.entity.vo.SnsUserRelationVO;
import com.baseframe.user.service.BaseUserService;
import com.baseframe.user.service.SnsUserRelationService;
import com.baseframe.user.mapper.SnsUserRelationMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**

* @description 针对表【sns_user_relation(用户关系表)】的数据库操作Service实现
* @createDate 2023-07-05 05:57:46
*/
@Service
@AllArgsConstructor
public class SnsUserRelationServiceImpl extends ServiceImpl<SnsUserRelationMapper, SnsUserRelation>
    implements SnsUserRelationService{

    private BaseUserService baseUserService;

    @Override
    public SnsUserRelationVO getRelation(SnsUserRelationRequest request) {


        SnsUserRelation snsUserRelation = getOne(new LambdaQueryWrapper<SnsUserRelation>().eq(SnsUserRelation::getUserId, AuthUtil.getUserId()).eq(SnsUserRelation::getFollowUserId, request.getFollowUserId()));

        SnsUserRelationVO snsUserRelationVO = new SnsUserRelationVO();

        snsUserRelationVO.setFollow(snsUserRelation!=null);

        return snsUserRelationVO;
    }

    @Override
    public List<BaseUserVO> getBuddyList() {
        List<SnsUserRelationVO> buddyList = getBaseMapper().getBuddyList(AuthUtil.getUserId());
        
        List<Long> userIds = new ArrayList<>();
          
        buddyList.forEach(e->{

            if(e.isFriend()){ //判断是不是互相关注 如果只是关注了就可以私信，只需要取消这个判断
                userIds.add(e.getUserId());
                userIds.add(e.getFollowUserId());
            }

        });
        Long userId = AuthUtil.getUserId();
        List<Long> friendsIds = userIds.stream().filter(e -> !Objects.equals(e, userId)).collect(Collectors.toList());
        List<BaseUserVO> userVOList = new ArrayList<>();
        if(friendsIds.size()!=0){
            //查询这些用户的信息
            List<BaseUser> users = baseUserService.list(new LambdaQueryWrapper<BaseUser>().in(BaseUser::getId,friendsIds));

            users.forEach(e->{
                BaseUserVO baseUserVO = new BaseUserVO();
                BeanUtils.copyProperties(e,baseUserVO);
                baseUserVO.setPassword(null);
                userVOList.add(baseUserVO);
            });
        }


        return userVOList;
    }

    @Override
    public boolean hasFollow(Long userId, Long followUserId) {
        SnsUserRelation snsUserRelation = getOne(new LambdaQueryWrapper<SnsUserRelation>().eq(SnsUserRelation::getUserId, userId).eq(SnsUserRelation::getFollowUserId, followUserId));
        return snsUserRelation!=null;
    }
}




